package com.javer.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;

import com.javer.dao.support.DataSourceDaoSupport;
import com.javer.dao.support.ResultUtils;
import com.javer.domain.Comment;
import com.javer.domain.PageView;

@Repository
public class CommentDao extends DataSourceDaoSupport {
	private static CommentMapper commentMapper = new CommentMapper();

	public void updateCommentForAgree(Comment c) {
		Assert.notNull(c, "comment object must not be null");
		jdbcTemplate.update("update comment set agree=agree+("+ c.getAgree() +"),disagree=disagree+("+ c.getDisagree() +") where id=" + c.getId());
	}
	
	public List<Comment> getNewComment(int limit) {
		return jdbcTemplate.query("select c.*, 1 orderNo from Comment c where ctype=0 order by id desc limit " + limit, commentMapper);
	}
	
	public List<Comment> getCommentsById(int id, int ctype) {
		return jdbcTemplate.query("select c.*,@row:=@row+1 as orderNo from Comment c join (select @row:=0) r where tid=" + id + " and ctype=" + ctype + " limit 500", commentMapper);
	}
	
	public List<Comment> getHotCommentsById(int id, int ctype) {
		return jdbcTemplate.query("select c.*,@row:=@row+1 as orderNo from Comment c join (select @row:=0) r where tid=" + id + " and ctype=" + ctype + " order by (agree+disagree) desc limit 5", commentMapper);
	}
	
	public void addComment(Comment comment) {
		Assert.notNull(comment, "comment must not be null");
		Object[] args = new Object[]{comment.getTid(), comment.getUid(), comment.getUip(), comment.getUaddress(),
				comment.getCtype(), comment.getContent()};
		jdbcTemplate.update("insert into comment(tid,uid,uip,uaddress,ctype,content,ctime) values(?,?,?,?,?,?,now())", args);
	}
	
	public PageView getPageByTid(PageView pv, int tid, String ctype) {
		String gettnamesql = "select cname from company where id=c.tid";
		if ("1".equals(ctype)) {
			gettnamesql = "select title from article where id=c.tid";
		}
		String sql = "select * from (select c.*,("+ gettnamesql +") tname,@row:=@row+1 as orderNo from comment c join (select @row:=0) r where tid="+ tid +" and ctype="+ ctype +") t2 order by id asc";
		return getByPage(pv, "select count(*) from comment where tid=" + tid + " and ctype=" + ctype, sql, commentMapper);
	}
	
	public PageView getByPage(PageView pv) {
		String sql = "select c.*,(select cname from company where id=c.tid) tname from comment c order by id desc";
		return getByPage(pv, "select count(*) from comment", sql, commentMapper);
	}
	
	static class CommentMapper implements RowMapper {

		public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
			Comment c = new Comment();
			c.setId(rs.getInt("id"));
			c.setTid(rs.getInt("tid"));
			c.setUid(rs.getInt("uid"));
			c.setUip(rs.getString("uip"));
			c.setUaddress(rs.getString("uaddress"));
			c.setContent(rs.getString("content"));
			c.setCtime(rs.getTimestamp("ctime"));
			c.setCtype(rs.getInt("ctype"));
			c.setAgree(ResultUtils.getInt(rs, "agree"));
			c.setDisagree(ResultUtils.getInt(rs, "disagree"));
			c.setOrderNo(ResultUtils.getInt(rs, "orderNo"));
			c.setTname(ResultUtils.getString(rs, "tname"));
			return c;
		}
		
	}
}
